package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.Callback;
import org.example.ttms.customize.BuyTicket;
import org.example.ttms.customize.Detail;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.customize.Window;
import org.example.ttms.entityClass.ticket.ChangeTicket;
import org.example.ttms.entityClass.ticket.RefundTicket;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Trips;

import java.util.List;
import java.util.Optional;

public class OrderController {

    @FXML
    private TableView<Ticket> tableView;

    @FXML
    private TableColumn<Ticket, String> tripsIdTableColumn;

    @FXML
    private TableColumn<Ticket, String> departureTableColumn;

    @FXML
    private TableColumn<Ticket, String> destinationTableColumn;

    @FXML
    private TableColumn<Ticket, String> goTimeTableColumn;

    @FXML
    private TableColumn<Ticket, String> arrivalTimeTableColumn;

    @FXML
    private TableColumn<Ticket, String> typeTableColumn;

    @FXML
    private TableColumn<Ticket, Double> priceTableColumn;

    @FXML
    private TableColumn<Ticket, String> seatIdTableColumn;

    @FXML
    private TableColumn<Ticket, String> statusTableColumn;

    @FXML
    private TableColumn<Ticket, Void> changeTableColumn;

    @FXML
    private TableColumn<Ticket, Void> refundTableColumn;

    @FXML
    private Label label;

    public void initialize(){
        Ticket ticket = new Ticket();
        List<Ticket> ticketList = ticket.obtainUserAllTicket(UserController.user.getName());
        ObservableList<Ticket> observableList = FXCollections.observableArrayList(ticketList);
        //tableView
        tripsIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().tripsIdProperty());
        departureTableColumn.setCellValueFactory(cellData -> cellData.getValue().departureProperty());
        destinationTableColumn.setCellValueFactory(cellData -> cellData.getValue().destinationProperty());
        goTimeTableColumn.setCellValueFactory(cellData -> cellData.getValue().goTimeProperty());
        arrivalTimeTableColumn.setCellValueFactory(cellData -> cellData.getValue().arrivalTimeProperty());
        typeTableColumn.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
        priceTableColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
        seatIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().seatIdProperty());
        statusTableColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
        changeTableColumn.setCellFactory(new Callback<TableColumn<Ticket, Void>, TableCell<Ticket, Void>>() {
            @Override
            public TableCell<Ticket, Void> call(TableColumn<Ticket, Void> ticketVoidTableColumn) {
                return new TableCell<Ticket, Void>(){
                    private final Button changeButton = new Button("  改签  ");
                    {
                        changeButton.setOnAction(e -> {
                            Ticket ticket1 = getTableView().getItems().get(getIndex());
                            ChangeTicket change = new ChangeTicket();
                            change.changeTicket(ticket1);
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(changeButton);
                        }
                    }
                };
            }
        });
        refundTableColumn.setCellFactory(new Callback<TableColumn<Ticket, Void>, TableCell<Ticket, Void>>() {
            @Override
            public TableCell<Ticket, Void> call(TableColumn<Ticket, Void> ticketVoidTableColumn) {
                return new TableCell<Ticket, Void>(){
                    private final Button refundButton = new Button("  退票  ");
                    {
                        refundButton.setOnAction(e -> {
                                Ticket ticket1 = getTableView().getItems().get(getIndex());
                                RefundTicket refund = new RefundTicket();
                                refund.refundTicket(ticket1);
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(refundButton);
                        }
                    }
                };
            }
        });


        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Ticket> c) -> {
            label.setVisible(observableList.isEmpty());
        } );
        tableView.setItems(observableList);
    }

}
